package function

import (
	_ "embed"
	"encoding/json"
	"math/rand"
	"time"
)

//go:embed countries.json
var countriesFile []byte

type Country struct {
	cn        string `json:"cn"`
	en        string `json:"en"`
	phoneCode string `json:"phone_code"`
}

type CountryFunction struct {
	size      int
	lang      string
	countries []map[string]string
	data      []interface{}
}

func DefaultCountry(size int, lang string) *CountryFunction {
	var countries []map[string]string
	json.Unmarshal(countriesFile, &countries)
	return &CountryFunction{
		size:      size,
		lang:      lang,
		countries: countries,
		data:      make([]interface{}, 0, size),
	}
}

func (c *CountryFunction) Build() []interface{} {
	rand.Seed(time.Now().Unix())
	for i := 0; i < c.size; i++ {
		c.data = append(c.data, c.countries[rand.Intn(len(c.countries))][c.lang])
	}
	return c.data
}
